﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace BeatEmUp.Animation
{
    class AnimationManager
    {
        Vector2 singleImageSize;
        int rowItemsCount;
        int totalItems;
        int displayedItems;
        float frameTime;
        float currentTime;
        int x, y;

        public AnimationManager(Vector2 singleImageSize, int rowImagesCount, int totalImages, float frameTime)
        {
            x = 0;
            y = 0;
            this.frameTime = frameTime;
            this.currentTime = frameTime;
            displayedItems = 2;
            this.singleImageSize = singleImageSize;
            this.rowItemsCount = rowImagesCount;
            this.totalItems = totalImages;
        }

        public Rectangle GetRectangle()
        {
            return new Rectangle(x * (int)singleImageSize.X, y * (int)singleImageSize.Y, (int)singleImageSize.X, (int)singleImageSize.Y);
        }

        //public bool IsLastFrame()
        //{
        //    if (displayedItems == totalItems)
        //        return true;
        //    return false;
        //}

        public int CurrentFrame
        {
            get
            {
                return x;
            }
        }

        public void UpdateFrame(GameTime gameTime)
        {
            currentTime = MathHelper.Max(0, currentTime - (float)gameTime.ElapsedGameTime.TotalMilliseconds);
            if (currentTime == 0)
            {
                x++;
                if (x >= rowItemsCount)
                {
                    x = 0;
                    y++;
                }
                if (displayedItems > totalItems)
                {
                    displayedItems = 1;
                    x = 0;
                    y = 0;
                }
                displayedItems++;
                currentTime = frameTime;
            }
        }
    }
}
